package edu.cicese.alz;

import edu.cicese.alz.swing.CustomButton;
import edu.cicese.alz.swing.SwingUtil;
import edu.cicese.alz.util.PropertyUtil;
import org.openstreetmap.gui.jmapviewer.MapHome;
import org.openstreetmap.gui.jmapviewer.MapViewer;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 14/07/12
 * Time: 03:17 PM
 */
public class FrameCoordinates extends SwingComponent {

	private FrameSubjects frmSubjects;
	private MapViewer pMap;

	private CustomButton btnAccept, btnCancel;

	public static void main(String[] a) {
		SwingUtil.setUIManager();
		new FrameCoordinates(null);
	}

	public FrameCoordinates(FrameSubjects frmSubjects) {
		super(800, 500, "Home coordinates");

		this.frmSubjects = frmSubjects;

		initComponents();
		setVisible(true);
	}

	private void initComponents() {
		double homeLat = PropertyUtil.getDouble(PropertyUtil.HOME_LATITUDE);
		double homeLon = PropertyUtil.getDouble(PropertyUtil.HOME_LONGITUDE);
		pMap = new MapViewer(new MapHome(homeLat, homeLon, 50));
		pMap.setDisplayPositionByLatLon(homeLat, homeLon, PropertyUtil.getInt(PropertyUtil.HOME_ZOOM));
		pMap.setCoordinate(homeLat, homeLon);
		pMap.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1),
				BorderFactory.createLineBorder(Color.WHITE, 4)));

		JPanel pContent = new JPanel(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();

		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.weightx = 0.5;
		gbc.weighty = 1;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc.insets = new Insets(5, 5, 5, 5);
		pContent.add(pMap, gbc);

		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.weightx = 0;
		gbc.insets = new Insets(5, 0, 5, 5);
		pContent.add(createButtonsBox(), gbc);

		pContent.setBackground(SwingUtil.cBackground);
		setContentPane(pContent);

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent e) {
				frmSubjects.toFront();
				frmSubjects.setEnabled(true);
			}
			public void windowClosing(WindowEvent e) {
				frmSubjects.setEnabled(true);
			}
		});
	}

	private Box createButtonsBox() {
		btnAccept = createCustomButton("", "Accept", "b_ok");
		btnAccept.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionAccept();
			}
		});

		btnCancel = createCustomButton("", "Cancel", "b_cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionCancel();
			}
		});

		Box boxButtons = Box.createVerticalBox();
		boxButtons.add(btnAccept);
		boxButtons.add(btnCancel);

		return boxButtons;
	}

	public void setCoordinates(double homeLat, double homeLon) {
		pMap.setCoordinate(homeLat, homeLon);
		pMap.setMapHome(new MapHome(homeLat, homeLon, 50));
		pMap.setDisplayPositionByLatLon(homeLat, homeLon, PropertyUtil.getInt(PropertyUtil.HOME_ZOOM));
		pMap.repaint();
	}

	private void actionAccept() {
		PropertyUtil.setProperty(PropertyUtil.HOME_LATITUDE, pMap.getCoordinate().getLat() + "");
		PropertyUtil.setProperty(PropertyUtil.HOME_LONGITUDE, pMap.getCoordinate().getLon() + "");
		int zoom = pMap.getZoom() >= pMap.getMinZoom() + 3 ? pMap.getZoom() - 3 : pMap.getZoom();
		PropertyUtil.setProperty(PropertyUtil.HOME_ZOOM, zoom + "");

		frmSubjects.setCoordinates(pMap.getCoordinate().getLat(), pMap.getCoordinate().getLon());
		closeWindow(true);
	}

	private void actionCancel() {
		closeWindow(true);
	}

	public void deselect() {
		btnAccept.paintDeselected();
		btnCancel.paintDeselected();
	}
}
